package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsRelatedDownload;
import com.ruoyi.system.domain.news.dto.NewsRelatedDownloadDto;
import com.ruoyi.system.domain.news.query.NewsRelatedDownloadQuery;
import com.ruoyi.system.domain.news.vo.NewsRelatedDownloadVo;
import com.ruoyi.system.service.news.NewsRelatedDownloadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsRelatedDownload")
@Api(value = "newsRelatedDownloadController", tags  = "测评工具下载")
public class NewsRelatedDownloadController extends BaseController {

	@Resource
	private NewsRelatedDownloadService newsRelatedDownloadService;

    @ApiOperation(value = "新增/修改测评工具下载信息", notes = "新增/修改测评工具下载信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsRelatedDownloadDto newsRelatedDownloadDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsRelatedDownload newsRelatedDownload = new NewsRelatedDownload();
		BeanUtils.copyProperties(newsRelatedDownloadDto,newsRelatedDownload);
		if (newsRelatedDownload.getId()==null) {
			newsRelatedDownload.setCreateBy(loginUser.getUsername());
			newsRelatedDownload.setDeptId(loginUser.getDeptId());
			newsRelatedDownload.setCreateTime(new Date());
			newsRelatedDownload.setUpdateBy(loginUser.getUsername());
			newsRelatedDownload.setUpdateTime(new Date());
		} else {
			newsRelatedDownload.setUpdateBy(loginUser.getUsername());
			newsRelatedDownload.setUpdateTime(new Date());
		}
		boolean result = newsRelatedDownloadService.saveOrUpdate(newsRelatedDownload);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除测评工具下载信息", notes = "删除测评工具下载信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsRelatedDownloadService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个测评工具下载信息", notes = "获取单个测评工具下载信息")
	@GetMapping("/getById")
	public R<NewsRelatedDownloadVo> getById(@RequestParam(value = "id") Long id) {
		NewsRelatedDownloadQuery newsRelatedDownloadQuery = new NewsRelatedDownloadQuery();
		newsRelatedDownloadQuery.setId(id);
		List<NewsRelatedDownloadVo> newsRelatedDownloadList = newsRelatedDownloadService.getNewsRelatedDownloadList(newsRelatedDownloadQuery);
		if (newsRelatedDownloadList!=null&&newsRelatedDownloadList.size()>0) {
			return R.ok(newsRelatedDownloadList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取测评工具下载信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsRelatedDownloadVo>> list(@RequestBody NewsRelatedDownloadQuery newsRelatedDownloadQuery) {
		List<NewsRelatedDownloadVo> newsRelatedDownloadList = newsRelatedDownloadService.getNewsRelatedDownloadList(newsRelatedDownloadQuery);
		return R.ok(new PageInfo<>(newsRelatedDownloadList));
	}
}
